package com.zhangxiaofanfan.hotcode.fourth.third;

import java.util.Arrays;

/**
 *
 * @date 2024-07-13 03:14:13
 * @author zhangxiaofanfan
 */
public class HotCode088 {
    public static void main(String[] args) {
        HotCode088 hotCode = new HotCode088();
        int[] nums = {-1,-2,-3,0};
        System.out.println(hotCode.maxProduct(nums));
    }

    public int maxProduct(int[] nums) {
        int[] dp = new int[2];
        Arrays.fill(dp, Integer.MIN_VALUE);
        int max = nums[0];
        for (int num : nums) {
            if (num == 0) {
                dp[0] = Integer.MIN_VALUE;
                dp[1] = Integer.MIN_VALUE;
                max = Math.max(max, 0);
            } else if (num > 0) {
                dp[0] = (dp[0] == Integer.MIN_VALUE ? 1 : dp[0]) * num;
                if (dp[1] != Integer.MIN_VALUE) {
                    dp[1] = dp[1] * num;
                }
                max = Math.max(max, dp[0]);
            } else {
                if (dp[0] == Integer.MIN_VALUE && dp[1] == Integer.MIN_VALUE) {
                    dp[1] = num;
                } else if (dp[0] == Integer.MIN_VALUE) {
                    dp[0] = dp[1] * num;
                    dp[1] = num;
                } else if (dp[1] == Integer.MIN_VALUE){
                    dp[1] = dp[0] * num;
                    dp[0] = Integer.MIN_VALUE;
                } else {
                    int temp = dp[0] * num;
                    dp[0] = dp[1] * num;
                    dp[1] = temp;
                }
                max = Math.max(dp[0], max);
            }
        }
        return max;
    }
}